import checkbox from '@inquirer/checkbox'
import { pick } from 'lodash-unified'

import { build, buildClient, buildServer } from './build.js'
import { check, checkClient, checkServer } from './check.js'
import { dev, devServer } from './dev.js'
import { genHash, genIcons, genTypes } from './gen.js'
import { lint, lintClient, lintServer } from './lint.js'
import { testClient, testServerE2E, testServerUnit } from './test.js'
import { logEnd, logStart, separator } from './util.js'

const answer = await checkbox({
  message: '选择脚本执行',
  choices: [
    { type: 'separator', separator: separator('All') },
    { name: 'dev', value: 'dev' },
    { name: 'build', value: 'build' },
    { name: 'check', value: 'check' },
    { name: 'lint', value: 'lint' },
    { type: 'separator', separator: separator('Client') },
    { name: 'build', value: 'buildClient' },
    { name: 'gen:icons', value: 'genIcons' },
    { name: 'gen:types', value: 'genTypes' },
    { name: 'test', value: 'testClient' },
    { name: 'check', value: 'checkClient' },
    { name: 'lint', value: 'lintClient' },
    { type: 'separator', separator: separator('Server') },
    { name: 'dev', value: 'devServer' },
    { name: 'build', value: 'buildServer' },
    { name: 'gen:hash', value: 'genHash' },
    { name: 'test:unit', value: 'testServerUnit' },
    { name: 'test:e2e', value: 'testServerE2E' },
    { name: 'check', value: 'checkServer' },
    { name: 'lint', value: 'lintServer' }
  ]
}).catch(err => {})

for (const [k, v] of Object.entries(
  pick(
    {
      dev,
      devServer,
      genHash,
      genIcons,
      genTypes,
      build,
      buildClient,
      buildServer,
      check,
      checkClient,
      checkServer,
      lint,
      lintClient,
      lintServer,
      testClient,
      testServerE2E,
      testServerUnit
    },
    answer!
  )
)) {
  console.group(k)
  logStart()

  await v()

  logEnd()
  console.groupEnd()
}
